package com.wh.hotel.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

/**
 * @author wenhao
 */
public class PageResultUtil {

    public static <T> IPage<T> page(List<T> dataList, int pageNum, int pageSize) {
        IPage<T> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNum);
        page.setTotal(CollectionUtils.size(dataList));
        if (CollectionUtils.isEmpty(dataList) || page.offset() >= page.getTotal()) {
            return null;
        }
        page.setRecords(dataList.subList((int)page.offset(), (int)Math.min(page.offset() + pageSize, page.getTotal())));
        return page;
    }

    public static <T, F> void copyPageInfo(IPage<T> source, IPage<F> target) {
        target.setCurrent(source.getCurrent());
        target.setPages(source.getPages());
        target.setSize(source.getSize());
        target.setTotal(source.getTotal());
    }
}
